<?php
namespace App\Http\Controllers\Admin;

use App\Model\PriceSettingMaster;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class PriceSettingController extends Controller
{
    /**
     * 価格設定マスタ
     * 一覧表示
     */
    function index(Request $request)
    {
       try{
            $pricesettingMaster = new PriceSettingMaster();

            $pricesettingInfo = $pricesettingMaster->getPriceSettingInfo();
            $pricesGroupIds = $pricesettingMaster->getPriceGroupId();
            $pricesGroupifos = array();
            for ($i = 0; $i < count($pricesGroupIds); $i ++) {
                $pricesGroup = $pricesettingMaster->getPriceGroupInfo($pricesGroupIds[$i]->group_id);
                $pricesGroupifos[] = [
                    $pricesGroupIds[$i]->group_id,
                    $pricesGroup
                ];
            }

            $allInfo = [
                $pricesettingInfo,
                $pricesGroupifos
            ];

            return view('admin.pricesetting_mst', array(
                'allInfo' => $allInfo
            ));
         }catch (\Exception $e){
             \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
       }
    }

    /**
     * 価格設定マスタ
     * 価格設定情報を登録する
     */
    function savePrice(Request $request)
    {
       try{
            $catIds = explode(',', $request->input('catIds')); // category_ids
            $groupId = $request->input('group_id');
            $priceseIfos = $request->input('priceseInfo'); // prices
            $groupIdold = $request->input('groupId');
            $pricesettingMaster = new PriceSettingMaster();

            if (empty($groupIdold)) {

                $flg = $pricesettingMaster->addPriceGroupInfo($groupId, $catIds, $priceseIfos);
            } else {
                $flg = $pricesettingMaster->udpPriceGroupInfo($groupIdold, $catIds, $priceseIfos);
            }

            if ($flg) {
                $mesIfo = "保存成功！";
            } else {
                $mesIfo = "保存失敗！";
            }
            session([
                'mesIfo' => $mesIfo
            ]);

            return redirect('/admin/setprice');
          }catch (\Exception $e){
             \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
       }
    }

    /**
     * 価格設定マスタ
     * 価格設定情報を削除する
     */
    function delPrice(Request $request)
    {
       try{
                $groupIdold = $request->input('groupId');
                $pricesettingMaster = new PriceSettingMaster();

                $flg = $pricesettingMaster->delPriceGroupInfo($groupIdold);

                if ($flg) {
                    $mesIfo = "削除成功！";
                } else {
                    $mesIfo = "削除失敗！";
                }
                session([
                    'mesIfo' => $mesIfo
                ]);
                return redirect('/admin/setprice');
           }catch (\Exception $e){
              \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }

    function getPriceByGrpId()
    {
        try{
                $code = $_POST['group_id'];
                $goodsQuery = new PriceSettingMaster();
                $goodsCode = $goodsQuery->getPriceByGroupId($code);
                return count($goodsCode);
           }catch (\Exception $e){
                \Log::error($e->getFile()." Line:".$e->getLine()." Msg:".$e->getMessage());
        }
    }
}

